$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/wantedusers/list',
        datatype: "json",
        colModel: [
            // {label: '序号', name: 'id', index: 'id', align: "center", width: 40, key: true},
            // {
            //     label: '用户ID', name: 'userId', index: 'user_id', align: "center", width: 60,
            //     formatter: function (value, options, rowData) {
            //         return "<a  onclick='window.open(\"../sys/buserinfo.html?userId=" + value + "\")'>" + value + "</a>";
            //     }
            // },
            {label: '昵称', name: 'nickName', index: 'nick_name', align: "center", width: 60},
            {
                label: '头像', name: 'avatarUrl', index: 'avatar_url', align: "center", width: 80,
                sortable: false,
                editable: false,
                formatter: function (value, options, rowData) {
                    var url = "https://lktvtool.oss-cn-shenzhen.aliyuncs.com/icon/236A5FF0-52DE-4644-B66C-E9555C97693D.png";
                    if (value == null || value == "") {
                        return "<img src='" + url + "' width='60'/>";
                    } else {
                        return "<img src='" + value + "' width='60'/>";
                    }
                }
            },
            // {label: 'openId', name: 'openId', index: 'open_id', align: "center", width: 100},
            // {label: '公众号', name: 'gzhCode', index: 'gzh_code', align: "center", width: 80},
            {label: '创建者', name: 'createBy', index: 'create_by', align: "center", width: 80},
            {label: '创建时间', name: 'createTime', index: 'create_time', align: "center", width: 80},
        ],
        viewrecords: true,
        height: 520,
        rowNum: 50,
        rowList: [15, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        wantedUsers: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.wantedUsers = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.wantedUsers.id == null ? "sys/wantedusers/save" : "sys/wantedusers/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.wantedUsers),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/wantedusers/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "sys/wantedusers/info/" + id, function (r) {
                vm.wantedUsers = r.wantedUsers;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});
